// Modal.vue
<template>
  <BasicModal
    v-bind="$attrs"
    @register="register"
    :title="displayModuleTitle"
    @ok="onOkButtonClick"
    width="520px"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>
<script lang="ts">
  import { computed, defineComponent, ref, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { displayTitle } from '/@/utils/QcUnifyUtil';
  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { BasicForm, useForm } from '/@/components/Form';
  import { manualOperateFormSchema } from './exchangeData.data';
  import { exchangeDataManualOperation } from '/@/api/qms/exchangeData';
  import { emit } from 'process';
  const { createMessage } = useMessage();
  export default defineComponent({
    components: { BasicModal, BasicForm },
    name: 'AddOrUpdateExchangeData',
    emits: ['success'],
    setup(props, { emit }) {
      const qcUnifyProps = ref<QcUnifyTransToChildProps>();
      const cid = ref();
      const stcd = ref();
      const [registerForm, { setFieldsValue, resetFields, validate, updateSchema }] = useForm({
        schemas: manualOperateFormSchema,
        labelWidth: 70,
        showActionButtonGroup: false,
      });
      const [register, { closeModal }] = useModalInner((data) => {
        if (data != undefined && data != null) {
          let unrefData = unref(data);
          qcUnifyProps.value = unrefData;
          //接收父组件传递的数据
          if (data) {
            cid.value = unrefData.cid;
            stcd.value = unrefData.stcd;
            console.log(unrefData.stcd, unrefData.stcdOption, 'unrefData.stcd');
            updateSchema({
              field: 'stcd',
              componentProps: {
                options: unrefData.stcdOption,
              },
            });
            setFieldsValue({
              stcd: unrefData.stcd,
            });
          }
        }
      });

      /**
       * 提交表单,交换数据人工操作
       */
      async function onOkButtonClick() {
        const values = await validate();
        exchangeDataManualOperation({
          ...values,
          endtm: values.begintm[1],
          begintm: values.begintm[0],
          cid: cid.value,
          ismanual: true,
        }).then((res) => {
          if (!res) {
            createMessage.success('操作成功');
            closeModal();
            emit('success');
          }
        });
      }

      let displayModuleTitle = computed(() => {
        return displayTitle(qcUnifyProps.value);
      });
      return {
        register,
        registerForm,
        closeModal,
        displayModuleTitle,
        onOkButtonClick,
      };
    },
  });
</script>
